export default {
  namespaced: true,

  state: {
    searchUserList: [],
    searchDiscussionList: [],
    searchTeamList: [],
  },
  mutations: {
    setUserList(state, value) {
      state.searchUserList = value;
    },
    setDiscussionList(state, value) {
      state.searchDiscussionList = value;
    },
    setTeamList(state, value) {
      state.searchTeamList = value;
    },
  },

  actions: {
    // 获取 / 查询用户
    getSearchUserList({ commit }, payload) {
      const db = wx.cloud.database(),
        _ = db.command,
        { name, type, page, limitCount } = payload;
      return new Promise(async (resolve, reject) => {
        try {
          if (type === "query") {
            const res = await db
              .collection("user")
              .where({
                nickName: {
                  $regex: ".*" + name,
                  $options: "i",
                },
              })
              .get();
            console.log("查询到用户:", res.data);
            commit("setUserList", res.data);
          } else {
            const res = await db
              .collection("user")
              .orderBy("createTime", "desc")
              .where(
                _.or([
                  {
                    nickName: _.neq(""),
                  },
                  {
                    avatar: _.neq(""),
                  },
                ])
              )
              .skip(page * limitCount)
              .limit(limitCount)
              .get();
            console.log("获取到用户:", res.data);
            commit("setUserList", res.data);
          }
          resolve("获取成功");
        } catch (err) {
          //TODO handle the exception
          console.log("查询列表错误：", err);
          reject(err);
        }
      });
    },

    // 获取 / 查询讨论
    getSearchDiscussionList({ commit }, payload) {
      const db = wx.cloud.database(),
        _ = db.command,
        { title, type, page, limitCount } = payload;
      return new Promise(async (resolve, reject) => {
        try {
          if (type === "query") {
            const res = await db
              .collection("discussion")
              .where({
                title: {
                  $regex: ".*" + title,
                  $options: "i",
                },
              })
              .get();
            console.log("查询到讨论:", res.data);
            commit("setDiscussionList", res.data);
          } else {
            const res = await db
              .collection("discussion")
              .orderBy("liked", "desc")
              .skip(page * limitCount)
              .limit(limitCount)
              .get();
            console.log("获取到讨论:", res.data);
            commit("setDiscussionList", res.data);
          }
          resolve("获取成功");
        } catch (err) {
          //TODO handle the exception
          console.log("查询列表错误：", err);
          reject(err);
        }
      });
    },

    // 获取 / 查询队伍
    getSearchTeamList({ commit }, payload) {
      const db = wx.cloud.database(),
        _ = db.command,
        { title, type, page, limitCount } = payload;
      return new Promise(async (resolve, reject) => {
        try {
          if (type === "query") {
            const res = await db
              .collection("team")
              .where({
                title: {
                  $regex: ".*" + title,
                  $options: "i",
                },
              })
              .get();
            console.log("查询到队伍:", res.data);
            commit("setTeamList", res.data);
          } else {
            const res = await db
              .collection("team")
              .orderBy("createTime", "desc")
              .skip(page * limitCount)
              .limit(limitCount)
              .get();
            console.log("获取到队伍:", res.data);
            commit("setTeamList", res.data);
          }
          resolve("获取成功");
        } catch (err) {
          console.log("查询列表错误：", err);
          reject(err);
        }
      });
    },
  },
};
